package com.gljr.exam.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.gljr.exam.dao.ExamPaperQuestionRepository;
import com.gljr.exam.dao.ExamResultRepository;
import com.gljr.exam.dao.TotalPointsRepository;
import com.gljr.exam.dao.UploadExamRepository;
import com.gljr.exam.domain.TotalPoints;
import com.gljr.exam.domain.UploadExam;
import com.gljr.exam.tools.ExamUniTools;
import com.gljr.exam.web.form.UploadExamForm;

@Controller
@SessionAttributes("user")
public class ExamController {

	@Autowired
	private ExamPaperQuestionRepository examPaperQuestionRepository;

	@Autowired
	private UploadExamRepository uploadExamRepository;

	@Autowired
	private ExamResultRepository examResultRepository;

	@Autowired
	private TotalPointsRepository totalPointsRepository;

	@RequestMapping(path = "/checkexam", method = RequestMethod.GET)
	public String checkExam(@ModelAttribute UploadExamForm form, Model model) {
		System.out.println("打印出sessionGET------>" + model.asMap().get("user"));
		if (model.asMap().get("user") == null) {
			model.addAttribute("err");
			return "redirect:/errpage";
		}
		return "checkexam";
	}

	@RequestMapping(path = "/checkexam", method = RequestMethod.POST)
	public String doCheckExam(@ModelAttribute UploadExamForm form, Model model) {
		System.out.println("打印出sessionPOST------>" + model.asMap().get("user"));
		if (model.asMap().get("user") == null) {
			model.addAttribute("err", "您未登录之前不可以验证题目题,请先登录!");
			return "redirect:/errpage";
		}
		Long userexamnb = Long.parseLong(ExamUniTools.generateTimeSuffix());// 考试试卷流水号,提交一次考试就变一次

		String[] arrUserOpt = new String[] { "", form.getExop1(), form.getExop2(), form.getExop3(), form.getExop4(),
				form.getExop5(), form.getExop6(), form.getExop7(), form.getExop8(), form.getExop9(), form.getExop10(),
				form.getExop11(), form.getExop12(), form.getExop13(), form.getExop14(), form.getExop15(),
				form.getExop16(), form.getExop17(), form.getExop18(), form.getExop19(), form.getExop20(),
				form.getExop21(), form.getExop22(), form.getExop23(), form.getExop24(), form.getExop25(),
				form.getExop26(), form.getExop27(), form.getExop28(), form.getExop29(), form.getExop30(),
				form.getExop31(), form.getExop32(), form.getExop33(), form.getExop34(), form.getExop35(),
				form.getExop36(), form.getExop37(), form.getExop38(), form.getExop39(), form.getExop40(),
				form.getExop41(), form.getExop42(), form.getExop43(), form.getExop44(), form.getExop45(),
				form.getExop46(), form.getExop47(), form.getExop48(), form.getExop49(), form.getExop50() };// 用户选择数组
		String currentUsername = ExamUniTools.getSessionUsername(model);
		String currentRealname = ExamUniTools.getSessionRealname(model);
		model.addAttribute("realname", currentRealname);

		List<Long> arrLong = form.getArrExamNum();

		for (Long long1 : arrLong) {
			System.out.println("用户题号数组---->" + long1);
		}

		for (int i = 1; i <= 50; i++) {

			String strexque1 = examPaperQuestionRepository.findExquestionByExnum2(arrLong.get(i));
			model.addAttribute("exque" + i, strexque1);
			String strrealkey1 = examPaperQuestionRepository.findExkeyByExnum2(arrLong.get(i));
			model.addAttribute("strrealkey" + i, strrealkey1);
			model.addAttribute("getExop" + i, arrUserOpt[i]);
			if (arrUserOpt[i] == null || arrUserOpt[i] == "") {
				model.addAttribute("err1" + i, "<p style=\"color:red\">答题错误,未选择!</p>");

			} else if (arrUserOpt[i].equals(strrealkey1)) {
				model.addAttribute("err1" + i, "<p style=\"color:green\">答题正确!</p>");
				uploadExamRepository.save(new UploadExam(currentUsername, 2L, arrUserOpt[i], userexamnb, true));
			} else if (!arrUserOpt[i].equals(strrealkey1)) {
				System.out.println("arrUserOpt[i]---->" + arrUserOpt[i]);
				model.addAttribute("err1" + i, "<p style=\"color:red\">答题错误!</p>");
				uploadExamRepository.save(new UploadExam(currentUsername, 0L, arrUserOpt[i], userexamnb, false));
			}
		}

		Long rsLong = examResultRepository.findResultByUserexamnb(userexamnb);// 当前试卷总分
		System.out.println("用户本次总分数---->" + rsLong);
		// TODO 保存每个人总分保存的为最近的分数
		if (totalPointsRepository.findByUsername(currentUsername) == null) {
			totalPointsRepository.save(new TotalPoints(rsLong, currentUsername, currentRealname, userexamnb));
		} else {
			totalPointsRepository.updateTotalPoints(currentUsername, rsLong, userexamnb);
		}

		model.addAttribute("uploadExamResult", rsLong);

		return "checkexam";
	}

}
